//
//  MBProgressHUD+showHUDs.m
//  ExcellentArtProject
//
//  Created by moduimen on 16/5/13.
//  Copyright © 2016年 apple. All rights reserved.
//

#import "MBProgressHUD+showHUDs.h"

@implementation MBProgressHUD (showHUDs)

/**
 *  显示过程处理信息
 *
 *  @param message 信息内容
 *
 *  @return MBProgressHUD, 需要手动关闭。注意事项: 若hud已创建，需在viewWillDisappear方法直接调用hideHUD方法。
 */
+ (MBProgressHUD *)showProgress:(NSString *)message
{
    return [self showProgress:message mode:MBProgressHUDModeIndeterminate toView:nil];
}

+ (MBProgressHUD *)showProgress:(NSString *)message mode:(MBProgressHUDMode)mode
{
    return [self showProgress:message mode:mode toView:nil];
}

/**
 *  显示过程处理信息
 *
 *  @param message 信息内容
 *  @param view    目标视图
 *
 *  @return MBProgressHUD, 需要手动关闭。注意事项: 若hud已创建，需在viewWillDisappear方法直接调用hideHUD方法。
 */
+ (MBProgressHUD *)showProgress:(NSString *)message toView:(UIView *)view {
    return [self showProgress:message mode:MBProgressHUDModeIndeterminate toView:view];
}

+ (MBProgressHUD *)showProgress:(NSString *)message mode:(MBProgressHUDMode)mode toView:(UIView *)view
{
    if (view == nil) {
        view = [UIApplication sharedApplication].keyWindow;
    }
    
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:view animated:YES];
    hud.label.text = message;
    hud.contentColor = [UIColor redColor];
    
    hud.mode = mode;
    hud.removeFromSuperViewOnHide = YES;
    hud.userInteractionEnabled = YES;
    return hud;
}

/**
 *  自定义view
 *
 *  @param text 信息内容
 *  @param icon 显示图标
 *  @param view 目标视图
 */
+ (void)show:(NSString *)text icon:(NSString *)icon view:(UIView *)view delay:(NSTimeInterval)dt
{
    __block UIView* blockView = view;
    dispatch_async(dispatch_get_main_queue(), ^{
        if (blockView == nil) {
            blockView = [UIApplication sharedApplication].keyWindow;
        }
        [self hideHUDForView:blockView];
        MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:blockView animated:YES];
        hud.label.text = text;
        hud.contentColor = [UIColor redColor];
        hud.customView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:icon]];
        
        hud.mode = MBProgressHUDModeCustomView;
        hud.removeFromSuperViewOnHide = YES;
        [hud hideAnimated:YES afterDelay:dt];
    });
}

/**
 *  显示成功信息
 *
 *  @param success 信息内容
 */
+ (void)showSuccess:(NSString *)success
{
    [self showSuccess:success toView:nil];
}

/**
 *  显示成功信息
 *
 *  @param success 信息内容
 *  @param view    目标视图
 */
+ (void)showSuccess:(NSString *)success toView:(UIView *)view
{
    [self show:success icon:@"done" view:view delay:0.7];
}

/**
 *  显示错误信息
 *
 */
+ (void)showError:(NSString *)error
{
    [self showError:error toView:nil];
}

/**
 *  显示错误信息
 *
 *  @param error 错误信息内容
 *  @param view  目标视图
 */
+ (void)showError:(NSString *)error toView:(UIView *)view{
    [self show:error icon:@"error" view:view delay:1.0];
}

/**
 *  显示提示信息
 *
 */
+ (void)showInfo:(NSString *)info
{
    [self showInfo:info toView:nil];
}

/**
 *  显示提示信息
 *
 *  @param info 提示信息内容
 *  @param view  目标视图
 */
+ (void)showInfo:(NSString *)info toView:(UIView *)view{
    [self show:info icon:@"info" view:view  delay:2.0];
}

/**
 *  显示文本信息
 *
 *  @param message  信息内容
 *  @param hide 是否需要自动隐藏
 *
 *  @return MBProgressHUD, autoHide为No时需手动关闭。注意事项: 若hud已创建，需在viewWillDisappear方法直接调用hideHUD方法。
 */
+ (MBProgressHUD *)showMessage:(NSString *)message autoHide:(BOOL)hide
{
    return [self showMessage:message autoHide:hide toView:nil];
}

/**
 *  显示文本信息
 *
 *  @param message  信息内容
 *  @param hide 是否需要自动隐藏
 *  @param view     目标视图
 *
 *  @return MBProgressHUD, autoHide为No时需手动关闭。注意事项: 若hud已创建，需在viewWillDisappear方法直接调用hideHUD方法。
 */
+ (MBProgressHUD *)showMessage:(NSString *)message autoHide:(BOOL)hide toView:(UIView *)view {
    if (view == nil) {
        view = [UIApplication sharedApplication].keyWindow;
    }

    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:view animated:YES];
    hud.label.text = message;

    hud.mode = MBProgressHUDModeText;
    hud.removeFromSuperViewOnHide = YES;
    if (hide) {
        [hud hideAnimated:YES afterDelay:1.0];
    }
    return hud;
}

/**
 *  手动关闭MBProgressHUD
 */
+ (void)hideHUD
{
    [self hideHUDForView:nil];
}

/**
 *  手动关闭MBProgressHUD
 *
 *  @param view    显示MBProgressHUD的视图
 */
+ (void)hideHUDForView:(UIView *)view
{
    if (view == nil) {
        view = [UIApplication sharedApplication].keyWindow;
    }
    
    [self hideHUDForView:view animated:YES];
}

@end
